package com.guli.produce.produce.service.impl;

import com.guli.produce.produce.dao.AttrGroupDao;
import com.guli.produce.produce.dao.CategoryDao;
import com.guli.produce.produce.entity.AttrGroupEntity;
import com.guli.produce.produce.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.produce.produce.dao.AttrDao;
import com.guli.produce.produce.entity.AttrEntity;
import com.guli.produce.produce.service.AttrService;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @SuppressWarnings("all")
    @Autowired
    private CategoryDao categoryDao;

    @SuppressWarnings("all")
    @Autowired
    private AttrGroupDao attrGroupDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params, String type, Long catelogId) {
        QueryWrapper<AttrEntity> w = new QueryWrapper<>();
        if (type.equals("sale")){
            w.eq("attr_type",0);
        }else{
            w.eq("attr_type",1);
        }

        if (!catelogId.equals(0l)){
            w.eq("catelog_id",catelogId);
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
               w
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> findattrGroupId(Long attrGroupId) {
        QueryWrapper<AttrEntity> w = new QueryWrapper<>();
        w.eq("attr_type",1);
        w.eq("attr_group_id",attrGroupId);

        return baseMapper.selectList(w);
    }

    @Override
    public PageUtils noattrRelation(Map<String, Object> params) {
        QueryWrapper<AttrEntity> w = new QueryWrapper<>();
            w.eq("attr_type",1);
        w.isNull("attr_group_id");
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                w
        );

        return new PageUtils(page);

    }

    //重写添加方法

    @Override
    public boolean save(AttrEntity entity) {
        //获取到每个值  一个是分组的 一个是分类的
        Long attrGroupId = entity.getAttrGroupId();
        Long catelogId = entity.getCatelogId();



        if (catelogId!=null && !catelogId.equals(0l)){
            CategoryEntity attrEntity1 =categoryDao .selectById(catelogId);
            entity.setCatelogName(attrEntity1.getName());
        }
        //根据id查询到的分类的名字和标识的名字
        if (attrGroupId!=null && !attrGroupId.equals(0l)){
            AttrGroupEntity attrEntity = attrGroupDao.selectById(attrGroupId);
            //查询到的对应名字赋值给
            entity.setAttrGroupName(attrEntity.getAttrGroupName());
        }
        return super.save(entity);
    }

    @Override
    public AttrEntity getById(Serializable id) {

        AttrEntity attrEntity = baseMapper.selectById(id);

        //创建集合
        List<Long> list=new ArrayList<>();

        Long catelogId = attrEntity.getCatelogId();

        path(list,catelogId);

        //将集合反转
        Collections.reverse(list);
        Long[] longs = list.toArray(new Long[list.size()]);
        attrEntity.setCatelogPath(longs);
        return attrEntity;
    }

    public void path(List<Long> list, Long catelogId) {
        //先把最后一个放进去
        list.add(catelogId);
        CategoryEntity en = categoryDao.selectById(catelogId);
        if (!en.getParentCid().equals(0l)){
            path(list,en.getParentCid());
        }

    }
}